﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using log4net;

namespace Tools.Framework
{
    /// <summary>
    /// Enterprise Library 日志记录
    /// </summary>
    public static class LogHelper
    {
        /// <summary>
        /// 静态构造函数
        /// </summary>
        static LogHelper()
        {

        }

        /// <summary>
        /// 用户日志
        /// </summary>
        public static void UserLog(this object obj, long userid, string content, string rmarkes = "")
        {
            try
            {
                
                var controller = obj as Controller;
                string ip = "";
                if (controller != null)
                {
                    ip = controller.Request.UserHostName + ",IP:" + controller.Request.UserHostAddress;
                }
               // var context = new ModelEntities();
                //context.OperationLog.Add(new OperationLog()
                //{
                //    UserId = (int)userid,
                //    UseIP =controller!=null?controller.Request.UserHostAddress:"",
                //    OperationTime = DateTime.Now,
                //    OperationSubModule = obj.GetType().FullName,
                //    OperationModule = obj.GetType().Namespace,
                //    OperationContent = content,
                //});
                //context.SaveChanges();
            }
            catch (Exception ex)
            {
                Write("用户操作日志写入错误！");
            }
        }



        /// <summary>
        /// 接口调用参数日志
        /// </summary>
        public static void InterfaceLog(HttpRequest request, string deptno, object paramters, string rmarkes = "")
        {
            try
            {
                var data = new { deptno = deptno, ip = request.UserHostAddress, paramters = paramters, time = DateTime.Now };
                Write(data.ToJsonString());
            }
            catch (Exception ex)
            {
                Write("接口调用参数日志写入错误！");
            }
        }


        /// <summary>
        /// 记录系统错误日志
        /// </summary>
        /// <param name="ex">Exception</param>
        /// <param name="logtype">日志类型</param>
        /// <param name="windowlog"></param>
        public static void Write(Exception ex, Log4NetErrorEnum logtype = Log4NetErrorEnum.错误, bool windowlog = false)
        {
            string message = "";
            if (ex.GetBaseException() != null)
                message += ex.GetBaseException().Message;
            Log4NetErrorLog(ex, logtype);
            if (windowlog)
                WindowsLogHelper.LogEvent(message, EventLogEntryType.Information);
        }

        /// <summary>
        /// 记录业务日志
        /// </summary>
        /// <param name="message">业务日志操作描述信息</param>
        /// <param name="logtype">日志类型</param>
        /// <param name="windowlog">是否写入windows 日志</param>
        public static void Write(string message,Log4NetErrorEnum logtype=Log4NetErrorEnum.一般信息,bool windowlog=false)
        {
            Log4NetErrorLog(new Exception(message), logtype);
            if (windowlog)
                WindowsLogHelper.LogEvent(message, EventLogEntryType.Information);
        }


        public static void Log4NetErrorLog(Exception ex, Log4NetErrorEnum errorEnum)
        {
            ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            switch (errorEnum)
            {
                case Log4NetErrorEnum.严重错误:
                    //记录严重错误
                    log.Fatal("fatal", ex);
                    break;
                case Log4NetErrorEnum.错误:
                    //记录错误日志
                    log.Error("error", ex);
                    break;
                case Log4NetErrorEnum.一般信息:
                    //记录一般信息
                    log.Info("info", ex);
                    break;
                case Log4NetErrorEnum.调试信息:
                    //记录调试信息
                    log.Debug("debug", ex);
                    break;
                case Log4NetErrorEnum.警告信息:

                    //记录警告信息
                    log.Warn("warn", ex);
                    break;
            }
        }
        public enum Log4NetErrorEnum
        {
            错误 = 1,//error
            严重错误 = 2,//fatal
            一般信息 = 3,//info
            调试信息 = 4,//debug
            警告信息 = 5//warn
        }
    }

}
